<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">
        当前位置：首页
      </el-breadcrumb-item>
      <el-breadcrumb-item>医院管理</el-breadcrumb-item>
      <el-breadcrumb-item>医院服务管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索区 -->
      <el-form
        ref="searchFormRef"
        :inline="true"
        :model="queryInfo.query"
        size="mini"
      >
        <el-form-item label="产品分类【ID】" prop="product_type_code">
          <el-select
            v-model="queryInfo.query.product_type_code"
            placeholder="请选择"
            filterable
            clearable
            @change="changeProductType"
          >
            <el-option
              v-for="item in selectData.product_type"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="产品名称【ID】" prop="product_id">
          <el-select
            v-model="queryInfo.query.product_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.product"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属医院【ID】" prop="hospital_id">
          <el-select
            v-model="queryInfo.query.hospital_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_hospital"
              :key="item.id"
              :label="item.title + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select
            v-model="queryInfo.query.status"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option key="0" label="关闭" value="0"> </el-option>
            <el-option key="1" label="开启" value="1"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            class="theme-background-color"
            size="mini"
            @click="searchList"
          >
            搜索
          </el-button>
          <el-button size="mini" @click="resetSearchForm"> 重置 </el-button>
        </el-form-item>
      </el-form>

      <!-- 操作区域 -->
      <div>
        <el-button
          class="theme-background-color"
          icon="el-icon-menu"
          size="mini"
          @click="$router.push('/hospital/ServiceAdd')"
        >
          新增服务
        </el-button>
        <el-button
          icon="el-icon-download"
          size="mini"
          :loading="btnLoading"
          @click="downloadExcel"
        >
          导出数据
        </el-button>
      </div>

      <!-- 用户列表区域 -->
      <el-table :data="list" border stripe>
        <!-- <el-table-column label="序号" type="index"></el-table-column> -->
        <el-table-column
          label="医院名称【ID】"
          min-width="150px"
          max-width="300px"
        >
          <template slot-scope="scope">
            {{ scope.row.hos_name }}【{{ scope.row.hospital_id }}】
          </template>
        </el-table-column>
        <el-table-column label="归属服务商【ID】">
          <template slot-scope="scope">
            {{ scope.row.provider_name }}【{{ scope.row.provider_id }}】
          </template>
        </el-table-column>
        <el-table-column
          label="服务ID"
          prop="provider_product_id"
        ></el-table-column>
        <el-table-column
          label="关联的产品名称【ID】"
          min-width="150px"
          max-width="300px"
          :show-overflow-tooltip="true"
        >
          <template slot-scope="scope">
            {{ scope.row.product_name }}【{{ scope.row.product_id }}】
          </template>
        </el-table-column>
        <el-table-column label="产品分类【ID】">
          <template slot-scope="scope">
            {{ scope.row.type_name }}【{{ scope.row.product_type_code }}】
          </template>
        </el-table-column>
        <el-table-column label="结算价" min-width="100px">
          <template slot-scope="scope">
            <span v-show="!scope.row.editPriceFlag">{{ scope.row.price }}</span>
            <i
              class="el-icon-edit"
              @click="scope.row.editPriceFlag = true"
              v-show="!scope.row.editPriceFlag"
              style="margin-left: 30px"
            ></i>
            <el-input
              v-focus="scope.row.editPriceFlag"
              v-model="scope.row.price"
              v-show="scope.row.editPriceFlag"
              @blur="changePrice(scope.row)"
              maxlength="8"
              v-bind:id="scope.row.id"
            ></el-input>
          </template>
        </el-table-column>
        <el-table-column label="状态" prop="status.name"></el-table-column>
        <el-table-column label="创建人【ID】">
          <template slot-scope="scope">
            {{ scope.row.create_name }}【{{ scope.row.create_id }}】
          </template>
        </el-table-column>
        <el-table-column label="创建时间" prop="create_time"></el-table-column>
        <el-table-column label="操作" width="80px">
          <template slot-scope="scope">
            <el-link
              type="danger"
              v-if="scope.row.status.id == 1"
              @click="disable(scope.row)"
            >
              关闭
            </el-link>
            <el-link
              type="primary"
              v-if="scope.row.status.id == 0"
              @click="enable(scope.row.id)"
            >
              开启
            </el-link>
            <el-link type="primary" @click="showLog(scope.row.id)">
              操作记录
            </el-link>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[10, 20, 50, 100]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>

      <el-dialog title="操作记录" :visible.sync="logDialogVisible" width="60%">
        <el-table :data="logTableData" border>
          <el-table-column
            prop="create_time"
            label="操作时间"
          ></el-table-column>
          <el-table-column
            prop="op_type.name"
            label="操作类型"
          ></el-table-column>
          <el-table-column prop="op_info" label="操作内容"></el-table-column>
          <el-table-column label="操作人【ID】">
            <template slot-scope="scope">
              {{ scope.row.op_uname }}【{{ scope.row.op_uid }}】
            </template>
          </el-table-column>
        </el-table>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: {
          product_type_code: '',
          product_id: '',
          hospital_id: '',
          status: ''
        },
        pagenum: 1,
        pagesize: 20
      },
      selectData: [],
      list: [],
      total: 0,
      btnLoading: false,

      //   操作记录
      logDialogVisible: false,
      logTableData: []
    }
  },
  created() {
    this.getSelectData()
    this.getListData()
  },
  methods: {
    async getSelectData() {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product,product_type,get_hospital',
        level: '2'
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    searchList() {
      this.queryInfo.pagenum = 1
      this.getListData()
    },
    resetSearchForm() {
      //   console.log(this)
      this.$refs.searchFormRef.resetFields()
      this.getListData()
    },
    async getListData() {
      // 返回数据重命名为res
      const { data: res } = await this.$http.post(
        '/admin/dr_hospital_ser/list',
        this.queryInfo
      )
      // console.log(res)
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      for (let i = 0; i < res.data.list.length; i++) {
        res.data.list[i].editPriceFlag = false
      }
      this.list = res.data.list
      this.total = res.data.total
    },
    // 监听 pagesize 改变的事件
    handleSizeChange(newSize) {
      console.log(newSize)
      this.queryInfo.pagesize = newSize
      this.getListData()
    },
    // 监听 页码值 改变的事件
    handleCurrentChange(newPage) {
      console.log(newPage)
      this.queryInfo.pagenum = newPage
      this.getListData()
    },
    async disable(row) {
      // 弹框询问是否关闭服务
      const confirmResult = await this.$confirm(
        '是否确认关闭该服务【' + row.id + '】？关闭后不能再使用',
        '关闭',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }
      ).catch((err) => err)

      if (confirmResult !== 'confirm') {
        return this.$message.info('已取消操作')
      }
      const { data: res } = await this.$http.post(
        '/admin/dr_hospital_ser/active',
        {
          id: row.id,
          type: 'close'
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    async enable(id) {
      const { data: res } = await this.$http.post(
        '/admin/dr_hospital_ser/active',
        {
          id: id,
          type: 'active'
        }
      )
      if (res.meta.status !== 200) {
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      this.getListData()
    },
    async downloadExcel() {
      this.btnLoading = true
      this.$http({
        method: 'post',
        url: '/admin/dr_hospital_ser/export',
        data: this.queryInfo.query,
        headers: {
          token: window.sessionStorage.getItem('token')
        },
        responseType: 'blob'
      })
        .then((res) => {
          this.btnLoading = false
          if (res.data.type) {
            // 文件下载
            const blob = new Blob([res.data], {
              type: 'application/vnd.ms-excel'
            })
            let link = document.createElement('a')
            link.href = URL.createObjectURL(blob)
            link.setAttribute('download', '医院服务数据.xlsx')
            link.click()
            link = null
            this.$message.success('导出成功')
          } else {
            // 返回json
            this.$message.warning(res.data.msg)
          }
        })
        .catch((err) => {
          this.btnLoading = false
          this.$message.error(err)
        })
    },
    // 修改结算价
    async changePrice(row) {
      const { data: res } = await this.$http.post(
        '/admin/dr_hospital_ser/editPrice',
        {
          service_item_id: row.id,
          price: row.price
        }
      )
      if (res.meta.status !== 200) {
        this.getListData()
        this.$message.error(res.meta.msg)
      }
      this.$message.success(res.meta.msg)
      row.editPriceFlag = false
      console.log(row)
    },
    async showLog(id) {
      const { data: res } = await this.$http.post(
        '/admin/dr_hospital_ser/getLog',
        {
          service_item_id: id
        }
      )

      this.logTableData = res.data
      //   接口请求数据
      this.logDialogVisible = true
    },
    async changeProductType(val) {
      const { data: res } = await this.$http.post('/admin/data/find', {
        code: 'product',
        type_code: val
      })
      // 返回结果
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData.product = res.data.product
    }
  },
  // 使用directives注册v-focus指令
  directives: {
    focus: {
      componentUpdated: function (el, { value }) {
        console.log(value)
        if (value) {
          console.log(el.querySelector('input'))
          el.querySelector('input').focus()
        }
      }
    }
  }
}
</script>

<style lang="less" scoped>
</style>
